import {Command} from '../constant';
import {IMainReducer} from '../types';
import {Action} from 'typings';
import produce from 'immer';
import * as immerUtil from '@/redux/immer-util';

const INITIAL_STATE: IMainReducer = {
  isReady: false,
  isLoading: false,
  status: {
    loadingIp: false,
    loadingGameInfo: false,
    loadingGameDaily: false,
    loadingUser: false,
    loadingGameTimes: false,
    loadingTask: false, // 查询任务列表
    loadingTaskDetail: false, // 查询任务详情
    computingTask: false,
    usingPropPossibility: false,
  },
  response: {
    loadIp: '',
    loadGameInfo: '',
    loadGameDaily: '',
    loadUser: '',
    loadGameTimes: '',
    loadTask: '',
    loadTaskDetail: '', // 查询任务详情
    computedTask: '',
    usePropPossibility: '', // 判断用户是否能使用道具
  },
  ipItems: [], // ip列表
  gameInfo: {}, // 游戏基本信息

  // 用户信息
  userInfo: {
    gameAvailableTimes: 0, // 游戏次数
  },
  gameReceiveInfo: {}, // 每日游戏收下
  task: {
    items: [],
    page: {
      total: 0,
      pageNum: 0,
      pageSize: 10000,
    },
    detail: {},
  },

  // 道具列表
  prop: {
    items: [],
    page: {
      total: 0,
      pageNum: 0,
      pageSize: 10,
    },
  },
  codeMessage: '',
};

export default function main(state = INITIAL_STATE, action: Action): IMainReducer {
  const {type, payload} = action;

  return produce<IMainReducer>(state, (draftState) => {
    switch (type) {
      //通用改变数据
      case Command.commonChange:
        return immerUtil.commonChange(draftState, {...payload, key: 'main'});

      //初始化
      case Command.init:
        draftState.isReady = true;
        for (let propKey in payload.main) {
          draftState[propKey] = payload.main[propKey];
        }
        return draftState;

      //重置
      case Command.clean:
        for (let propKey in INITIAL_STATE) {
          draftState[propKey] = INITIAL_STATE[propKey];
        }
        return draftState;
    }
  });
}
